<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #default="{ filterForm, handleQuery }"> </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['duty:message:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="[]"
    >
      <template #default>
        <el-table-column
          label="值班日期"
          align="center"
          key="dutyTime"
          prop="dutyTime"
          width="200"
        ></el-table-column>
        <el-table-column label="总值班" align="center">
          <el-table-column label="白班" align="center">
            <el-table-column
              label="人员"
              align="center"
              key="sumWhite"
              prop="sumWhite"
            ></el-table-column>
            <el-table-column
              label="电话"
              align="center"
              key="sumWhitePhone"
              prop="sumWhitePhone"
              width="150"
            ></el-table-column>
          </el-table-column>
          <el-table-column label="夜班" align="center">
            <el-table-column
              label="人员"
              align="center"
              key="sumBlack"
              prop="sumBlack"
            ></el-table-column>
            <el-table-column
              label="电话"
              align="center"
              key="sumBlackPhone"
              prop="sumBlackPhone"
              width="150"
            ></el-table-column>
          </el-table-column>
        </el-table-column>
        <el-table-column label="安保值班（24小时）" align="center">
          <el-table-column
            label="人员"
            align="center"
            key="security"
            prop="security"
          ></el-table-column>
          <el-table-column
            label="电话"
            align="center"
            key="securityPhone"
            prop="securityPhone"
            width="150"
          ></el-table-column>
        </el-table-column>
        <el-table-column label="公司领导" align="center">
          <el-table-column
            label="人员"
            align="center"
            key="companyLeader"
            prop="companyLeader"
          ></el-table-column>
          <el-table-column
            label="电话"
            align="center"
            key="companyLeaderPhone"
            prop="companyLeaderPhone"
            width="150"
          ></el-table-column>
        </el-table-column>
        <el-table-column
          label="创建人"
          align="center"
          key="createdBy"
          prop="createdBy"
        />
        <el-table-column
          label="创建时间"
          align="center"
          key="createdDate"
          prop="createdDate"
          width="200"
        />
        <el-table-column
          label="更新时间"
          align="center"
          key="modifyDate"
          prop="modifyDate"
          width="200"
        />
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['duty:message:edit']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['duty:message:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['fileSlot']"
      :title="dialogType == 'add' ? '新增值班人员信息' : '修改值班人员信息'"
    >
      <template #fileSlot>
        <el-row :gutter="12">
          <el-col :span="24">
            <el-form-item label="附件上传" prop="fileId">
              <FileUpload
                :limit="1"
                :fileSize="30"
                :fileType="['doc', 'docx', 'pdf']"
                v-model="files"
              ></FileUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/yjzhgl/zbryxx.js";
import formJson from "./components/options.json";
import FileUpload from "@/components/FileUpload";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const { proxy } = getCurrentInstance();
const showSearch = ref(true);

const options = reactive({
  table: ops,
  formOps: ops.filter((i) => ["dutyTime", "planName"].includes(i.prop)),
});
const files = ref([]);
const table = ref();
const dialogVform = ref();
watch(
  () => files,
  (newData) => {
    console.log(newData);
    if (newData.value.length === 0) {
      form.value.fileName = "";
      form.value.fileId = "";
      form.value.fileUrl = "";
    } else {
      form.value.fileName = newData.value[0].name;
      form.value.fileId = newData.value[0].id;
      form.value.fileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);
const plugEdit = (row) => {
  if (row.fileId) {
    files.value = [
      {
        name: row.fileName,
        id: row.fileId,
        url: row.fileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};
const handDownload = (row) => {
  window.downLoadFile(row.fileUrl, row.fileName);
};
const {
  onSubmit,
  fetchData,
  dialogType,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  form,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
});
</script>

<style lang="scss" scoped></style>
